package cn.com.fuhuitong.main.home.ui.activity

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.lifecycle.Observer
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.WebConfig
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.home.entity.CategoryTwoList
import cn.com.fuhuitong.main.home.ui.fragment.HomeCategoryFragment
import cn.com.fuhuitong.main.home.vm.CaseViewModel
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.activity_home_category.*

@Route(path = ARouteConstance.ACTIVITY_URL_CATEGORY)
class HomeCategoryActivity : ViewModeActivity<CaseViewModel>() {

    private val mCategoryTitle: String
        get() = intent.getStringExtra(AppConstance.BUNDLE_CATEGORY_TITLE) ?: ""

    private val mCategoryId: Int
        get() = intent.getIntExtra(AppConstance.BUNDLE_CATEGORY_ID, (0))

    override fun viewModel(): CaseViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_home_category

    override fun initView() {

        image_category_back.setOnClickListener {
            this.finish()
        }
        text_category_title.text = mCategoryTitle

        text_category_apply.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_WEB_VIEW)
                .withSerializable(
                    AppConstance.BUNDLE_WEB_VIEW,
                    MainBean(type = WebConfig.UNKNOWN.apply {
                        desc = text_category_apply.text.toString()
                    }, data = text_category_apply.tag.toString())
                )
                .navigation()
        }

    }

    override fun initData() {
        viewModel.indexGetTwoHomeGoods(mCategoryId)
    }

    override fun initViewMode() {
        viewModel.categoryTwoLiveData.observe(this, Observer { response ->
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            text_category_apply.isEnabled = response.response?.data?.button?.button_switch ?: 0 == 1
            text_category_apply.text = response.response?.data?.button?.button_name
            text_category_apply.tag = response.response?.data?.button?.button_url
            val categoryTwoList: MutableList<CategoryTwoList> = response.response?.data?.list
                ?: mutableListOf()
            fillData(categoryTwoList)
        })
    }

    private fun fillData(categoryTwoList: MutableList<CategoryTwoList>) {

        tab_category_title.visibility =
            if (categoryTwoList.size == 0) View.GONE else View.VISIBLE

        tab_category_title.setupWithViewPager(vp_category_body.apply {
            adapter = object : FragmentStatePagerAdapter(
                supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
            ) {
                override fun getItem(position: Int): Fragment {
                    val homeIconId: Int = mCategoryId
                    val twoHomeIconId: Int = if (categoryTwoList.size == 0)
                        0 else categoryTwoList[position].id
                    return HomeCategoryFragment.newInstance(homeIconId, twoHomeIconId)
                }

                override fun getCount(): Int =
                    if (categoryTwoList.size == 0) 1 else categoryTwoList.size

                override fun getPageTitle(position: Int): CharSequence {
                    return if (categoryTwoList.size == 0) "" else categoryTwoList[position].title
                }

                override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
            }
        })
    }

}